前面文章在處理訊息文字,這裡我要做的功能是在群組內喊要什麼東西+1,資料庫能把它紀錄下來
這邊在建一個資料表來記錄+1的userid跟LINE名字
一樣設mid為FK關聯到group_buying_message
group_buying_user:
id|mid|uid|name|quantity
---|---
我要讓人在關鍵字後加上要的數量關鍵字 A+1 B+2
關鍵字 +1
亦可使用LINE emoji關鍵字 (A)+1 (B)+2
整體功能做出來了,細節下篇做修飾
關鍵字與上傳至資料庫,傳資料庫的訊息到聊天室中
insert如果放前面的話,整段跑下來會超過1秒,訊息會被拒絕,所以在insert前就先送出訊息,而未insert入的資料,在前面直接加入message_text_d
if(event.message.text[:3:] in keywords and len(event.message.text)>3):
key=event.message.text[:3:]
profile = line_bot_api.get_profile(event.source.user_id)
cursor=conn.cursor()
cursor.execute(f"SELECT message_text FROM group_buying_message WHERE keyword='{key}';")
message_text = cursor.fetchone()
cursor.execute(f"SELECT index,product_id,emoji_id FROM message_emoji WHERE mid=(SELECT mid FROM group_buying_message WHERE keyword='{key}');")
rows = cursor.fetchall()
emojis=[]
#將資料一筆一筆寫入list中
for row in rows:
emojis.append({'index': row[0],'productId': row[1],'emojiId': row[2]})
message_text_d="".join(message_text)
cursor.execute(f"SELECT name,quantity FROM group_buying_user WHERE mid=(SELECT mid FROM group_buying_message WHERE keyword='{key}');")
users = cursor.fetchall()
for user in users:
message_text_d=message_text_d+"".join(user[0])+" "+"".join(user[1])+"\n"
message_text_d=message_text_d+profile.display_name+" "+event.message.text[4::]
message=TextSendMessage(message_text_d,emojis)
line_bot_api.reply_message(event.reply_token,message)
cursor.execute(f"INSERT INTO group_buying_user (mid, uid, name, quantity) VALUES ((SELECT mid FROM group_buying_message WHERE keyword='{key}'),'{event.source.user_id}','{profile.display_name}','{event.message.text[4::]}' );")
conn.commit()
cursor.close()